/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.webservice
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.business.WebserviceDelegate;
	import multimedia.control.event.LoadDFXPXMLEvent;
	import multimedia.control.event.LoadImageEvent;
	import multimedia.control.event.LoadSlideEvent;
	import multimedia.control.event.LoadTextEvent;
	import multimedia.control.event.LoadVideoEvent;
	import multimedia.control.event.LoadViewerXMLEvent;
	import multimedia.control.event.SetNextMediaElementEvent;
	import multimedia.control.event.UpdateRssFeedPlayStatusEvent;
	import multimedia.control.event.webservice.NextEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.ImageVO;
	import multimedia.model.vo.MediaVO;
	import multimedia.model.vo.SlideVO;
	import multimedia.model.vo.TextVO;
	import multimedia.model.vo.VideoVO;
	import multimedia.util.LogUtil;
	
	import mx.logging.ILogger;
	import mx.rpc.IResponder;
	import mx.utils.StringUtil;
	
	/**
	 * 	NextCommand
	 * 
	 *	@author jemden
	 *	@version 1.0
	 */
	public class NextCommand extends SequenceCommand implements ICommand, IResponder
	{
		private static const LOG:ILogger = LogUtil.getLogger(NextCommand);
		private var model:ModelLocator;
		
		private var e:NextEvent;
		
		public static var called:Boolean = false;
		
		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = event as NextEvent;
			this.nextEvent = e.nextEvent;
			
			//model.currentPlayerState = "";
			var service:WebserviceDelegate = new WebserviceDelegate(this);
			service.next(e.apiKey, e.playerId);
		}
		
		public function result(data:Object):void
		{
			if(called)
			{
				return;
			}	
			called = true;
			LOG.debug("<<< Got Result from NEXT-Command ({0})>>>", data.result..mediaId);
			model.log = "NextCommand "+data.result..mediaId;
			var media:MediaVO;
			
			var mediaId:String = data.result..mediaId;
			var type:String = data.result..type;
			var title:String = data.result..title;		
			var validDate:Number = data.result..validDate;		
			var newsticker:Boolean = data.result..newsticker == "true";		
			var duration:Number = data.result..duration;			
			var reference:String = data.result..reference;
			var title_color:uint = data.result..title_color;

			this.nextEvent = new UpdateRssFeedPlayStatusEvent(newsticker, this.nextEvent);
			
			switch(type)
			{
				case "text":
					media = new TextVO(); 
				break;		
				case "video":
					media = new VideoVO(); 
				break;
				case "image":
					media = new ImageVO();
				break;
				case "slide":
					media = new SlideVO(); 
				break;		
			}
			
			if(media != null)
			{
				media.mediaId = mediaId;
				media.type = type;
				media.title = title;
				media.validDate = validDate;
				media.newsticker = newsticker;
				media.duration = duration;
				media.reference = reference;
				media.title_color = title_color;
				
				//model.newsRSS.visible = media.newsticker;
				
				switch(type)
				{
					case "text":
						var text:TextVO = TextVO(media);
						
						var textString:String = data.result..text;
						var formatedText:String = data.result..formatedText;
						var backgroundColor:uint = data.result..background_color;
						var skin:String = data.result..skin;
						var video_url:String = data.result..video;
						var video_aspect_ratio:String = data.result..video_aspect_ratio;
						var image:String = data.result..image;
						var image_source:String = data.result..image_source;
						var image_orientation:String = data.result..image_orientation;	
						var image_width:Number = data.result..image_width;
						var image_height:Number = data.result..image_height;
						var image_aspect_ratio:Number = data.result..image_aspect_ratio;
						var background_image:String = data.result..background_image;
						var margin_bottom:Number = data.result..margin_bottom;
						var margin_left:Number = data.result..margin_left;
						var margin_top:Number = data.result..margin_top;
						var margin_right:Number = data.result..margin_right;
						var publish_date:String = data.result..publish_date;
						var hide_title:Boolean = data.result..hide_title == "true";
						
						text.text = textString;
						text.formatedText = formatedText;
						text.backgroundColor = backgroundColor;
						text.skin = skin;
						text.video = video_url;
						text.video_aspect_ratio = video_aspect_ratio;
						text.image = image;
						text.image_source = image_source;
						text.image_orientation = image_orientation;
						text.image_width = image_width;
						text.image_height = image_height;
						text.image_aspect_ratio = image_aspect_ratio;
						text.background_image = background_image;
						text.margin_bottom = margin_bottom;
						text.margin_left = margin_left;
						text.margin_top = margin_top;
						text.margin_right = margin_right;
						text.publish_date = publish_date;
						text.hide_title = hide_title;
						
						model.currentText = text;
						new LoadTextEvent(text, this.nextEvent).dispatch();
					break;		
					case "video":
					
					model.log +="\nsetting video stuff";
						var video:VideoVO = VideoVO(media);
						
						var videoUrl:String = StringUtil.trim(data.result..url);
						var subtitles:String = data.result..subtitles;
						var volume:Number = data.result..volume;
						var inPoint:Number = data.result..inPoint;
						var outPoint:Number = data.result..outPoint;
						var video_aspect_ratio2:String = data.result..aspect_ratio;
						
						video.url = videoUrl;
						video.subtitles = subtitles;
						video.volume = volume;
						video.inPoint = inPoint;
						video.outPoint = outPoint;
						video.aspect_ratio = video_aspect_ratio2;
						
						model.currentVideo = video;
						
						new LoadDFXPXMLEvent(video, 
							new LoadVideoEvent(video, this.nextEvent)).dispatch();
					break;
					case "slide":
						var slide:SlideVO = SlideVO(media);
						
						var slideUrl:String = data.result..url;
						var slide_aspect_ratio:String = data.result..aspect_ratio;
			
						slide.url = slideUrl;
						slide.aspect_ratio = slide_aspect_ratio;
						
						model.currentSlide = slide;
						
						new LoadViewerXMLEvent(slide,
							new LoadSlideEvent(slide, 1, this.nextEvent)).dispatch();
						break;
					case "image":
						var images:ImageVO = ImageVO(media);
						var image_image_source:String = data.result..image_source;
						var image_image_aspect_ratio:Number = data.result..aspect_ratio;
						var image_url:String = data.result..url;
						
						images.image_source = image_image_source;
						images.aspect_ratio = image_image_aspect_ratio;
						images.url = image_url;
						new LoadImageEvent(images, this.nextEvent).dispatch();
						break;
					default:
						model.log += "Kein Dateityp:" + type;
				}
			}
			else
				model.log = "NextCommand Error passender Dateityp nicht gefunden";
		}
		
		public function fault(info:Object):void
		{
			LOG.error("<<< ERROR when trying to get next media {0} >>>", info);
			LOG.error("<<< Errornum: {0} >>>", model.errors);
			model.log += "\nNextCommand Error:";
			model.errors++;
			model.allErrors++;
			if(model.errors >= 5)
			{
				new SetNextMediaElementEvent().dispatch();
				return;
			}
			NextCommand.called = false;
			new NextEvent(model.apiKey, model.playerId,
				new SetNextMediaElementEvent()).dispatch();
		}
	}
}